﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Data.SQLite;

/// <summary>
///UDCPage Basic Page
/// </summary>
public class UDCPage : System.Web.UI.Page
{
    public int Uid
    {
        get {
            ReLoginIn();
            return Int32.Parse(Session["LoginUser"].ToString());
        }
        set { Session["LoginUser"] = value; }
    }

    public UDCPage() { }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        ReLoginIn();
    }

    protected int GetDid()
    {
        int did = -1;
        string sqlstr = "select did from ud_user_t where uid = :uid";
        SQLiteParameter[] spc = {
                                new SQLiteParameter(":uid", DbType.UInt32)
                                };
        spc[0].Value = Uid;
        Int32.TryParse(SQLiteHelper.ExecuteScalar(sqlstr, spc).ToString(),out did);
        return did;
    }

    protected int GetRid()
    {
        int rid = -1;
        string sqlstr = "select rid from ud_user_role_t where uid = :uid";
        SQLiteParameter[] spc = {
                                new SQLiteParameter(":uid", DbType.UInt32)
                                };
        spc[0].Value = Uid;
        Int32.TryParse(SQLiteHelper.ExecuteScalar(sqlstr, spc).ToString(), out rid);
        return rid;
    }

    public void LoginOut()
    {
        Session["LoginUser"] = null;
    }

    private void ReLoginIn()
    {
        if (Session["LoginUser"] == null)
        {
            Response.Redirect("~");
            Response.End();
        }
    }

    protected bool IsAuditor()
    {
        string sqlstr = "select count(1) from ud_user_role_t where uid = :uid and rid = 3";
        SQLiteParameter[] spc = {
                                new SQLiteParameter(":uid", DbType.UInt32)
                                };
        spc[0].Value = Uid;
        int res = Int32.Parse(SQLiteHelper.ExecuteScalar(sqlstr, spc).ToString());
        return res > 0;
    }

    protected bool HaveAudited()
    {
        string sqlstr = @"select count(1)
                          from ud_saleinfo_t t1
                         where date(t1.saledate) = date('now','localtime','-1 day')  
                           and isaudit = 1
                           and exists( select 1 from ud_developer_t t2 
                                       where t1.dvid = t2.dvid
                                       and exists( select 1 from ud_reported_t t3 
                                                   where t2.did = t3.did
                                                   and date(t3.repdate) = date('now','localtime','-1 day'))) ";
        return Int32.Parse(SQLiteHelper.ExecuteScalar(sqlstr, null).ToString()) > 0;
    }

    protected string GetDFname()
    {
        string sqlstr = "select dfname from ud_depart_t where did = :did";
        SQLiteParameter[] spc = {
                                new SQLiteParameter(":did", DbType.UInt32)
                                };
        spc[0].Value = GetDid();
        return SQLiteHelper.ExecuteScalar(sqlstr, spc).ToString();
    }
}